package com.zhongyouyun.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.DeptModel;
import com.zhongyouyun.admin.mapper.DeptMapper;
import com.zhongyouyun.admin.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyouyun.utils.TreeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @Author: shenyi
 * @Date: 2021-12-17
 * @Description: 服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DeptServiceImpl extends ServiceImpl<DeptMapper, DeptModel> implements DeptService {
    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<DeptModel> selectPage(Page<DeptModel> page, QueryWrapper<DeptModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }
    @Override
    public List<DeptModel> findALLDeptTree() {
        List<DeptModel> deptModelList = this.baseMapper.findDeptTreeList(null);
        deptModelList.forEach(deptModel1 -> {
            deptModel1.setParentName("顶级部门");
            deptModelList.forEach(deptModel2 -> {
                if (deptModel1.getPId().equals(deptModel2.getId())) {
                    deptModel1.setParentName(deptModel2.getDeptName());
                }
            });
        });
        List<DeptModel> list = TreeUtil.listToTree(deptModelList);
        return list;
    }

    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<DeptModel> selectList(QueryWrapper<DeptModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<DeptModel> findDeptTree(DeptModel deptModel) {
        List<DeptModel> deptModelList = this.baseMapper.findDeptTreeList(deptModel);
        deptModelList.forEach(deptModel1 -> {
            deptModel1.setParentName("顶级部门");
            deptModelList.forEach(deptModel2 -> {
                if (deptModel1.getPId().equals(deptModel2.getId())) {
                    deptModel1.setParentName(deptModel2.getDeptName());
                }
            });
        });
        List<DeptModel> list = TreeUtil.listToTree(deptModelList);
        return list;
    }



    public String getoid(Long deptId) {
        return this.baseMapper.getoid(deptId);
    }

    public String getchild(Long deptId) {
        return this.baseMapper.getchild(deptId);
    }
}
